﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Texfina.Entity.Sy;
using Texfina.Entity.Mg;
using Texfina.Entity.Rh;
using Texfina.Core.Common;
using Texfina.Core.Data;
using System.Drawing;
using System.Drawing.Printing;
using Texfina.Common.Mg;
using MGBOQry = Texfina.BOQry.Mg;
using SYBOQry = Texfina.BOQry.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using RHBORpt = Texfina.BORpt.Rh;
using System.Data;

namespace TexfinaPrint
{
    /// <summary>
    /// Interaction logic for Printing.xaml
    /// </summary>
    public partial class Printing : UserControl
    {
        private Font printFont;
        private int _indexRow = 0;
        private int _NumberPage = 0;

        public DataTable DataSource
        {
            get;
            set;
        }

        public DataTable DataSourceddPlanilla
        {
            get;
            set;
        }

        public Printing()
        {
            InitializeComponent();
            LoadListControl();
        }

        public void LoadListControl()
        {
            ETablaGenDet objEDet = new ETablaGenDet();
            ECCosto objECCosto = new ECCosto();

            IBOQuery objEmp = WCFHelper.GetObject<IBOQuery>(typeof(SYBOQry.Empresa));
            EEmpresa objEEmp = new EEmpresa();
            DataTable dt = objEmp.GetList(objEEmp);

            ddidEmpresa.DataContext = dt;
            ddidEmpresa.DisplayMemberPath = dt.Columns[1].ToString();
            ddidEmpresa.SelectedValuePath = dt.Columns[0].ToString();

            IBOQuery objBOCCosto = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.CCosto));
            MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

            objEDet.IdTabla = ConstMultitabla.MES;
            dt = objBOI.GetDetailByList(objEDet);

            ddidMes.DataContext = dt;
            ddidMes.DisplayMemberPath = dt.Columns[2].ToString();
            ddidMes.SelectedValuePath = dt.Columns[1].ToString();

            objEDet.IdTabla = ConstMultitabla.FORMA_PAGO;
            dt = objBOI.GetDetailByList(objEDet);

            ddidForPago.DataContext = dt;
            ddidForPago.DisplayMemberPath = dt.Columns[2].ToString();
            ddidForPago.SelectedValuePath = dt.Columns[1].ToString();
        }

        private void ddidEmpresa_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ddidEmpresa.SelectedValue != null)
            {
                GetData_ddiPlanilla();
                SYBOQry.IEmpresa objBOEmp = (SYBOQry.IEmpresa)WCFHelper.GetObject<SYBOQry.IEmpresa>(typeof(SYBOQry.Empresa));
                EEmpEstable objEEmp = new EEmpEstable();
                objEEmp.IdEmpresa = ddidEmpresa.SelectedValue.ToString();
                DataTable dt = objBOEmp.GetEstablecimientos(objEEmp);

                ddidEstablecimiento.DataContext = dt;
                ddidEstablecimiento.DisplayMemberPath = dt.Columns[2].ToString();
                ddidEstablecimiento.SelectedValuePath = dt.Columns[1].ToString();

                IBOQuery objBOUNego = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.UNegocio));
                EUNegocio objEUNeg = new EUNegocio();
                objEUNeg.IdEmpresa = ddidEmpresa.SelectedValue.ToString();
                dt = objBOUNego.GetList(objEUNeg);

                ddUNegocio.DataContext = dt;
                ddUNegocio.DisplayMemberPath = dt.Columns[2].ToString();
                ddUNegocio.SelectedValuePath = dt.Columns[1].ToString();
            }
        }

        private void ddidMes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ddidMes.SelectedValue != null)
            {
                GetData_ddiPlanilla();
            }
        }

        private void ddidForPago_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ddidForPago.SelectedValue != null)
            {
                GetData_ddiPlanilla();
            }
        }
       
       public void GetData_ddiPlanilla()
        {
            EProcPlaniBandeja objE = new EProcPlaniBandeja();

            objE.IdPeriodo = "2012";
            objE.IdEmpresa = ddidEmpresa.SelectedValue != null ? ddidEmpresa.SelectedValue.ToString() : "";
            objE.IdMes = ddidMes.SelectedValue != null ? ddidMes.SelectedValue.ToString() : "";
            objE.IdForPago = ddidForPago.SelectedValue != null ? ddidForPago.SelectedValue.ToString() : "";

            objE.IdPlanilla = null;
            objE.DsPlanilla = null;

            RHBOQry.IProcPlani objBO = (RHBOQry.IProcPlani)WCFHelper.GetObject<RHBOQry.IProcPlani>(typeof(RHBOQry.ProcPlani));
            DataTable dt = objBO.GetByListWPF(objE);
            DataSourceddPlanilla = dt;
            ddidPlanilla.DataContext = dt;
            ddidPlanilla.DisplayMemberPath = dt.Columns[7].ToString();
            ddidPlanilla.SelectedValuePath = dt.Columns[0].ToString();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
           if (ddidEmpresa.SelectedValue == null)
           {              
              MessageBox.Show("Seleccione la Empresa");
              ddidEmpresa.Focus();
              return;
           }

           if (ddidMes.SelectedValue == null)
           {
              MessageBox.Show("Seleccione el Mes");
              ddidMes.Focus();
              return;
           }

           if (ddidForPago.SelectedValue == null)
           {
              MessageBox.Show("Seleccione la Forma de Pago");
              ddidForPago.Focus();
              return;
           }

           if (ddidPlanilla.SelectedValue == null)
           {
              MessageBox.Show("Seleccione la Planilla");
              ddidPlanilla.Focus();
              return;
           }

           if (txtnuProcAnualIni.Text=="")
           {
              MessageBox.Show("Ingreso el N° proceso desde");
              txtnuProcAnualIni.Focus();
              return;
           }

           if (txtnuProcAnualFin.Text == "")
           {
              MessageBox.Show("Ingreso el N° proceso hasta");
              txtnuProcAnualFin.Focus();
              return;
           }

            _indexRow = 0;
            _NumberPage = 1;
            DataSet ds = (DataSet)this.GetDataReport();
            DataSource = ds.Tables[0];


            if (0 ==this.DataSource.Rows.Count)
            { 
               MessageBox.Show("No se encontraron Datos");
               return;
            }

            printFont = new Font("Draft", 9);
            PrintDocument pd = new PrintDocument();
            pd.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("new size", txtancho.Text == "" ? 1063 : int.Parse(txtancho.Text), txtalto.Text == "" ? 550 : int.Parse(txtalto.Text));
            pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
            pd.Print();

        }

        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 6;
            float yPos = 0;
            int count = 0;
            float leftMargin = 0;
            float topMargin = 22;
            string stridEmpleado = null;
            string strDecValue = "";
            RectangleF drawRect;
            StringFormat stringFormat = new StringFormat();
            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

            DataRow row = null;

            if (_indexRow < this.DataSource.Rows.Count)
            {
                row = this.DataSource.Rows[_indexRow];
                stridEmpleado = row["idPersonal"].ToString();
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));

                ev.Graphics.DrawString(row["dsTipoTrab"].ToString(), new Font("Draft", 8), Brushes.Black, 900, yPos - 18);

                string nuProceso = "";
                if (txtnuProcAnualIni.Text == txtnuProcAnualFin.Text)
                   nuProceso = "  [ " + row["nuProcAnual"].ToString() + " ] ";
                else
                   nuProceso = "";
                ev.Graphics.DrawString(row["dsPlanilla"].ToString() + nuProceso + " DESDE : " + (row["feDesde"].ToString() != "" ? DateTime.Parse(row["feDesde"].ToString()).ToString("dd/MM/yyyy") : row["feDesde"].ToString()) + "   -   HASTA : " + (row["feHasta"].ToString() != "" ? DateTime.Parse(row["feHasta"].ToString()).ToString("dd/MM/yyyy") : row["feHasta"].ToString()), printFont, Brushes.Black, 300, yPos);

                ev.Graphics.DrawString(_NumberPage.ToString(), printFont, Brushes.Black, 900, yPos-6);
                ev.Graphics.DrawString(ddidMes.Text + " - 2012", new Font("Draft", 8), Brushes.Black, 900, yPos + 10);           

                topMargin = 48;
                count++;
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));

                ev.Graphics.DrawString(stridEmpleado, printFont, Brushes.Black, 10, yPos);
                ev.Graphics.DrawString(row["dsRazSocial"].ToString(), printFont, Brushes.Black, 90, yPos);

                drawRect = new RectangleF(485, yPos, 280, printFont.GetHeight(ev.Graphics) * 2);
                stringFormat.Alignment = StringAlignment.Near;
                ev.Graphics.DrawString(row["dsOcupa"].ToString(), new Font("Draft", 8), Brushes.Black, drawRect, stringFormat);

                ev.Graphics.DrawString(row["nuDocIden"].ToString(), printFont, Brushes.Black, 800, yPos);
                ev.Graphics.DrawString(row["idCCosto"].ToString(), printFont, Brushes.Black, 920, yPos);
                ev.Graphics.DrawString("C.COSTO", new Font("Draft", 8), Brushes.Black, 910, yPos + 27);

                topMargin = 85;
                count++;
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));

                ev.Graphics.DrawString(row["feIngpla"].ToString() != "" ? DateTime.Parse(row["feIngpla"].ToString()).ToString("dd/MM/yyyy") : row["feIngpla"].ToString(), printFont, Brushes.Black, 10, yPos);
                ev.Graphics.DrawString(row["mtRemu"].ToString(), printFont, Brushes.Black, 460, yPos);
                ev.Graphics.DrawString(row["dsAfp"].ToString(), printFont, Brushes.Black, 580, yPos);
                ev.Graphics.DrawString("REGIMEN", new Font("Draft", 8), Brushes.Black, 595, yPos + 23);

                ev.Graphics.DrawString(row["nuCuspp"].ToString(), printFont, Brushes.Black, 850, yPos);

                DataRow[] dr = this.DataSource.Select("idPersonal = '" + row["idPersonal"].ToString() + "'");

                if (dr.Length > 0)
                    foreach (DataRow drNew in dr)
                    {
                        topMargin = 150;
                        count++;
                        yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                        leftMargin = 5;
                        ev.Graphics.DrawString(drNew["dsConRemuRem"].ToString(), printFont, Brushes.Black, leftMargin, yPos);

                        if (drNew["nuHorasRem"].ToString() != null && drNew["nuHorasRem"].ToString() != "")
                        {
                            drawRect = new RectangleF(180, yPos, 100, printFont.GetHeight(ev.Graphics));
                            stringFormat.Alignment = StringAlignment.Far;
                            strDecValue = Math.Round(decimal.Parse(drNew["nuHorasRem"].ToString()), 2).ToString();
                            ev.Graphics.DrawString(strDecValue, printFont, Brushes.Black, drawRect, stringFormat);
                        }

                        if (drNew["mtCalculoRem"].ToString() != null && drNew["mtCalculoRem"].ToString() != "")
                        {
                            drawRect = new RectangleF(290, yPos, 100, printFont.GetHeight(ev.Graphics));
                            stringFormat.Alignment = StringAlignment.Far;
                            strDecValue = Math.Round(decimal.Parse(drNew["mtCalculoRem"].ToString()), 2).ToString();
                            ev.Graphics.DrawString(strDecValue, printFont, Brushes.Black, drawRect, stringFormat);
                        }

                        ev.Graphics.DrawString(drNew["dsConRemuRD"].ToString(), printFont, Brushes.Black, 430, yPos);

                        if (drNew["mtCalculoRetDed"].ToString() != null && drNew["mtCalculoRetDed"].ToString() != "" && drNew["mtCalculoRetDed"].ToString() != "0.0000")
                        {
                            drawRect = new RectangleF(640, yPos, 100, printFont.GetHeight(ev.Graphics));
                            stringFormat.Alignment = StringAlignment.Far;
                            strDecValue = Math.Round(decimal.Parse(drNew["mtCalculoRetDed"].ToString()), 2).ToString();
                            ev.Graphics.DrawString(strDecValue, printFont, Brushes.Black, drawRect, stringFormat);
                        }

                        if (drNew["mtCalculoApor"].ToString() != null && drNew["mtCalculoApor"].ToString() != "" && drNew["mtCalculoApor"].ToString() != "0.0000")
                        {
                            drawRect = new RectangleF(860, yPos, 100, printFont.GetHeight(ev.Graphics));
                            stringFormat.Alignment = StringAlignment.Far;
                            strDecValue = Math.Round(decimal.Parse(drNew["mtCalculoApor"].ToString()), 2).ToString();
                            ev.Graphics.DrawString(strDecValue, printFont, Brushes.Black, drawRect, stringFormat);
                        }

                        _indexRow++;
                    }
                else
                    _indexRow++;

                count = 1;
                topMargin = 410;
                count++;
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));

                drawRect = new RectangleF(290, yPos, 100, printFont.GetHeight(ev.Graphics));
                stringFormat.Alignment = StringAlignment.Far;
                ev.Graphics.DrawString(Math.Round(decimal.Parse(row["mtTotRemuMn"].ToString()), 2).ToString(), printFont, Brushes.Black, drawRect, stringFormat);

                drawRect = new RectangleF(640, yPos, 100, printFont.GetHeight(ev.Graphics));
                stringFormat.Alignment = StringAlignment.Far;
                ev.Graphics.DrawString(Math.Round(decimal.Parse(row["mtTotDescMn"].ToString()), 2).ToString(), printFont, Brushes.Black, drawRect, stringFormat);

                drawRect = new RectangleF(860, yPos, 100, printFont.GetHeight(ev.Graphics));
                stringFormat.Alignment = StringAlignment.Far;
                ev.Graphics.DrawString(Math.Round(decimal.Parse(row["mtTotAportMn"].ToString()), 2).ToString(), printFont, Brushes.Black, drawRect, stringFormat);

                count++;
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));

                drawRect = new RectangleF(290, yPos, 100, printFont.GetHeight(ev.Graphics));
                stringFormat.Alignment = StringAlignment.Far;
                ev.Graphics.DrawString(Math.Round(decimal.Parse(row["mtRedActMn"].ToString()), 2).ToString(), printFont, Brushes.Black, drawRect, stringFormat);

                drawRect = new RectangleF(640, yPos, 100, printFont.GetHeight(ev.Graphics));
                stringFormat.Alignment = StringAlignment.Far;
                ev.Graphics.DrawString(Math.Round(decimal.Parse(row["mtRedAntMn"].ToString()), 2).ToString(), printFont, Brushes.Black, drawRect, stringFormat);

                drawRect = new RectangleF(860, yPos, 100, printFont.GetHeight(ev.Graphics));
                stringFormat.Alignment = StringAlignment.Far;
                ev.Graphics.DrawString(Math.Round(decimal.Parse(row["mtNetoMn"].ToString()), 2).ToString(), printFont, Brushes.Black, drawRect, stringFormat);

            }
            _NumberPage++;

            // If more lines exist, print another page.
            if (_indexRow < this.DataSource.Rows.Count)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }

        public object GetDataReport()
        {

            EBoletaPago objEDet = new EBoletaPago();
            RHBORpt.IPlanilla objBODet = (RHBORpt.IPlanilla)WCFHelper.GetObject<RHBORpt.IPlanilla>(typeof(RHBORpt.Planilla));
            objEDet.IdPeriodo = "2012";
            objEDet.IdEmpresa = ddidEmpresa.SelectedValue.ToString();
            objEDet.IdForPago = ddidForPago.SelectedValue.ToString();


            DataRow[] dr = DataSourceddPlanilla.Select("RowNumber = " + ddidPlanilla.SelectedValue.ToString());

            if (dr.Length > 0)
               objEDet.IdPlanilla = dr[0]["idPlanilla"].ToString();
            else
               objEDet.IdPlanilla = null;
           
            if (ddidEstablecimiento.SelectedValue != null)
                objEDet.IdEstable = ddidEstablecimiento.SelectedValue.ToString();
            else
                objEDet.IdEstable = null;


            if (ddUNegocio.SelectedValue != null)
                objEDet.IdUNegocio = ddUNegocio.SelectedValue.ToString();
            else
                objEDet.IdUNegocio = null;

            if (ddidCCosto.SelectedValue != null)
                objEDet.IdCCosto = ddidCCosto.SelectedValue.ToString();
            else
                objEDet.IdCCosto = null;

            if (txtidPersonalIni.Text == "")
                objEDet.IdPersonalIni = null;
            else
                objEDet.IdPersonalIni = txtidPersonalIni.Text;
           
            if (txtidPersonalFin.Text == "")
               if (txtidPersonalIni.Text == "")
               
                   objEDet.IdPersonalFin = null;
               else

                  objEDet.IdPersonalFin = txtidPersonalIni.Text;

            else
                objEDet.IdPersonalFin = txtidPersonalFin.Text;


            if (txtnuProcAnualIni.Text == "")
               objEDet.NuProcAnualIni = 0;
            else
               objEDet.NuProcAnualIni = int.Parse(txtnuProcAnualIni.Text);


            if (txtnuProcAnualFin.Text == "")
               objEDet.NuProcAnualFin = 0;
            else
               objEDet.NuProcAnualFin = int.Parse(txtnuProcAnualFin.Text);

            DataSet ds = new DataSet();

            DataTable dt = null;

            dt = objBODet.GetBoletaTrabajadorPrint(objEDet);
            ds.Tables.Add(dt);
            return ds;


        }

        private void ddUNegocio_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (ddUNegocio.SelectedValue != null)
            {
                EUNegocio objEUNeg = new EUNegocio();
                MGBOQry.IUNegocio objBOUNego = (MGBOQry.IUNegocio)WCFHelper.GetObject<MGBOQry.IUNegocio>(typeof(MGBOQry.UNegocio));
                objEUNeg.IdEmpresa = ddidEmpresa.SelectedValue.ToString();
                objEUNeg.IdUNegocio = ddUNegocio.SelectedValue.ToString();
                DataTable dt = objBOUNego.GetCCostoXUNegocio(objEUNeg);

                ddidCCosto.DataContext = dt;
                ddidCCosto.DisplayMemberPath = dt.Columns[3].ToString();
                ddidCCosto.SelectedValuePath = dt.Columns[2].ToString();

            }


        }

        private void MenuItemClear_Click(object sender, RoutedEventArgs e)
        {
            ddidEstablecimiento.SelectedIndex = -1;
            ddUNegocio.SelectedIndex = -1;
            ddidCCosto.SelectedIndex = -1;
            txtidPersonalIni.Text = "";
            txtidPersonalFin.Text = "";
        }

        private void MenuItemClearAll_Click(object sender, RoutedEventArgs e)
        {
           ddidEmpresa.SelectedIndex = -1;
           ddidForPago.SelectedIndex = -1;
           ddidMes.SelectedIndex = -1;
           ddidPlanilla.SelectedIndex = -1;

           ddidEstablecimiento.SelectedIndex = -1;
           ddUNegocio.SelectedIndex = -1;
           ddidCCosto.SelectedIndex = -1;
           txtidPersonalIni.Text = "";
           txtidPersonalFin.Text = "";
        }

        private void ddidPlanillaIni_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           if (ddidPlanilla.SelectedValue != null)
           {

              DataRow[] dr = DataSourceddPlanilla.Select("RowNumber = " + ddidPlanilla.SelectedValue.ToString());

              if (dr.Length > 0)
              {
                 txtnuProcAnualIni.Text = dr[0]["nuProcAnual"].ToString();
                 txtnuProcAnualFin.Text = dr[0]["nuProcAnual"].ToString();
              }
              else
              {
                 txtnuProcAnualIni.Text = "0";
                 txtnuProcAnualFin.Text = "0";
              }
           }
        }

    }
}
